package com.mlnx.web.ui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.data.service.CheckDataService;
import com.mlnx.device.service.CheckDeviceService;
import com.mlnx.ui.entity.Patient;
import com.mlnx.ui.pojo.form.EditPatientForm;
import com.mlnx.ui.pojo.form.PatientFilterForm;
import com.mlnx.ui.pojo.form.PatientForm;
import com.mlnx.ui.pojo.vo.PatientVO;
import com.mlnx.ui.service.CheckUIService;
import com.mlnx.ui.service.PatientService;
import com.sun.management.OperatingSystemMXBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;

@Api(value = "管理后台接口", tags = "管理后台接口")
@RestController
@RequestMapping("manage")
public class ManageController extends BaseController {
    @Autowired
    private PatientService patientService;
    @Autowired
    private CheckUIService checkUIService;
    @Autowired
    private CheckDataService checkDataService;
    @Autowired
    private CheckDeviceService checkDeviceService;

    private Map<String, Boolean> serverState = new HashMap<>();

    @ApiOperation(value = "添加患者")
    @PostMapping(value = "/patient")
    public Response addPatient(@Valid PatientForm patientForm) {
        Integer patientId = patientService.addPatient(patientForm);

        return result(patientId);
    }


    @ApiOperation(value = "分页获取患者")
    @GetMapping(value = "/patient")
    public ResponseData<Page<PatientVO>> list(@Valid PageForm pageForm, PatientFilterForm patientFilterForm) {
        return result(patientService.list(pageForm, patientFilterForm));
    }

    @ApiOperation(value = "修改患者信息")
    @PutMapping(value = "/patient/{id}")
    public Response editPatient(@PathVariable("id") Integer id, @Valid EditPatientForm editPatientForm) {
        patientService.updatePatient(id, editPatientForm);

        return result();
    }

    @ApiOperation(value = "批量删除患者")
    @DeleteMapping(value = "/patient")
    public Response editPatient(Integer[] ids) {

        patientService.deletePatients(ids);

        return result();
    }

    @ApiOperation(value = "获取内存使用率")
    @GetMapping(value = "/memory")
    public Response memoryState() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        long totalMemorySize = osmxb.getTotalPhysicalMemorySize();
        long usedMemory = osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize();

        return result((float) usedMemory / totalMemorySize);
    }

    @ApiOperation(value = "获取CPU使用率")
    @GetMapping(value = "/cpu")
    public Response cpuState() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

        return result(osmxb.getSystemCpuLoad());
    }

    @ApiOperation(value = "获取注册患者数量")
    @GetMapping(value = "/patient/count")
    public Response patientCount() {
        return result(patientService.list(new QueryWrapper<Patient>().eq("alive", 1)).size());
    }

    @ApiOperation(value = "获取服务器开启状态")
    @GetMapping(value = "/server")
    public Response checkServers() {
        serverState.put("UI服务", checkUIService.checkServer());
        serverState.put("数据服务", checkDataService.checkServer());
        serverState.put("设备服务", checkDeviceService.checkServer());

        return result(serverState);
    }


}
